class DepositsController < ApplicationController

  before_filter :check_cookies
  before_filter :login_required
  
  cache_sweeper :balance_sweeper
  
  sidebar :deposits, :if => :logged_in?
  sidebar :ajax_module_balans, :if => :logged_in?

  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @deposit_pages, @deposits = paginate :deposits, :per_page => 10
  end

  def show
    @deposit = Deposit.find(params[:id])
  end

  def new
    @deposit = Deposit.new
    @users = User.find(:all)
  end
  
  def create
    @deposit = Deposit.new(params[:deposit])
    @deposit.payer = current_user
    
    if @deposit.save
      flash[:notice] = 'De storting is opgeslagen.'
      redirect_to :controller => 'dashboard'
    else
      render :action => 'new'
    end
  end
  
  def update
    raise 'Not yet implemented'
    @deposit.attributes = params[:deposit]
  end
  
  def my
    @deposits_from_user = current_user.deposits_from
    @deposits_to_user = current_user.deposits_to
  end

  def edit
    @deposit = Deposit.find(params[:id])
    @users = User.find(:all)
  end

  def destroy
    Deposit.find(params[:id]).destroy
    redirect_to :action => 'list'
  end

  def confirm
	#Get the id's
	ids = params[:id]

	if (ids.nil?)
	  flash[:notice] = "Als u een storting wilt goedkeuren, moet u er wel minimaal 1 selecteren"
	  return(redirect_to :back)
	end
  
	#Loop through all ids
	ids.each_value { |id|
	  deposit = Deposit.find(id)
	  deposit.update_attribute "confirmed", "true"
	}

	#Return to the previous page
	flash[:message] = "Stortingen goedgekeurd"
	redirect_to :back

  end

private

end
